<template>
  <el-form :model="loginForm" :rules="fieldRules" ref="loginForm" label-position="left" label-width="0px"
           class="demo-ruleForm login-container">
    <span class="tool-bar">
    </span>
    <h2 class="title">登录</h2>
    <el-form-item prop="account">
      <el-input type="text" v-model="loginForm.account" auto-complete="off" placeholder="账号"></el-input>
    </el-form-item>
    <el-form-item prop="password">
      <el-input type="password" v-model="loginForm.password" auto-complete="off" placeholder="密码"></el-input>
    </el-form-item>
    <el-form-item style="width:100%;">
      <el-button type="primary" style="width:47%;" @click="$router.push('/registerm')">没有账号？</el-button>
      <el-button type="primary" style="width:47%;" @click.native.prevent="login" :loading="loading">登 录</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
  import Cookies from "js-cookie"

  export default {
    name: 'Login',
    data() {
      return {
        loading: false,
        loginForm: {
          account: '',
          password: '',
        },
        fieldRules: {
          account: [  // 字母+数字
            {required: true, message: '请输入账号', trigger: 'blur'},
            {pattern: /^[A-Za-z0-9]{1,16}$/, message: '16个以内字母或数字', trigger: ['blur', 'change']}
          ],
          password: [ // 六到十六位
            {required: true, message: '请输入密码', trigger: 'blur'},
            {min:6, max:16 , message: '6~16位字符', trigger: ['blur', 'change']}
          ]
        }
      }
    },
    methods: {
      login() {
        this.loading = true
        this.$refs["loginForm"].validate((valid) => {
          if (valid) {
            let userInfo = {
              username: this.loginForm.account, password: this.loginForm.password
            }
            this.$api.user.dologin(userInfo).then((res) => {
              // console.log(res)
              if (res.error != null) {
                this.$message({message: res.error, type: 'error'})
              } else {
                Cookies.set('token', res.token) // 放置token到Cookie
                Cookies.set('role', res.role)
                sessionStorage.setItem('user', userInfo.account) // 保存用户到本地会话
                if (res.role === 'super') {
                  this.$router.push('/adminpage')  // 如果是管理员就重定向到管理员页面
                }else{
                  this.$router.push('/mypagem')
                }
              }
              this.loading = false
            }).catch((res) => {
              this.$message({message: res.message, type: 'error'})
            })
            this.loading = false
          } else {
            console.log('error submit!!');
            this.$message({message: '请检查输入格式', type: 'warning'})
            this.loading = false
            return false
          }
        });
        this.loading = false
      }
      ,
      reset() {
        this.$refs.loginForm.resetFields()
      }
    },
    mounted() {
      Cookies.set('token', null) // 放置token到Cookie
      Cookies.set('role','annoym')
    }
  }
</script>

<style lang="scss" scoped>
  .login-container {
    -webkit-border-radius: 5px;
    border-radius: 5px;
    -moz-border-radius: 5px;
    background-clip: padding-box;
    margin: 100px auto;
    width:260px;
    padding: 35px 35px 15px 35px;
    background: #fff;
    border: 1px solid #eaeaea;
    box-shadow: 0 0 25px #cac6c6;

    .title {
      margin: 0 auto 30px auto;
      text-align: center;
      color: #505458;
    }
  }
</style>
